package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName AdChannelController
 * @Description TODO
 * @Author 10725
 * @Date 2021/9/19 20:32
 **/
@Api(value = "频道管理API接口", tags = "", description = "管理频道相关接口")
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {

    @Autowired
    private AdChannelService channelService;


    /**
     * 根据名称分页查询频道列表
     */
    @ApiOperation(value = "频道分页列表查询", notes = "按照name模糊查询,分页查询,按照order升序排序")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto) {
        return channelService.findByNameAndPage(dto);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "频道新增")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel channel) {
        return channelService.insert(channel);
    }

    @ApiOperation(value = "频道修改")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel channel) {
        return channelService.update(channel);
    }


    @ApiOperation("根据频道ID删除")
    @GetMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
        //任意代码手动抛出自定义异常，如下是在控制器的删除方法中抛出
        if(true){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return channelService.deleteById(id);
    }


    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }
}
